#coding=utf-8
from django.contrib import admin
from django.core.exceptions import MultipleObjectsReturned
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from PublicTool import HttpWrite
from PublicTool.BaseAdmin import UserModelAdmin
import models
__author__ = 'czq'
class ProvinceAdmin(admin.ModelAdmin):
    fields = ('name',)
admin.site.register(models.Province,ProvinceAdmin)


class CityAdmin(admin.ModelAdmin):
    fields = ('name','province','cityCode',)
admin.site.register(models.City,CityAdmin)

class DistrictAdmin(admin.ModelAdmin):
    fields = ('name','city',)
admin.site.register(models.District,DistrictAdmin)

class PositionTypeAdmin(UserModelAdmin):
    fields = ('name','rank','type',)
admin.site.register(models.PositionType,PositionTypeAdmin)

class LineTypeAdmin(UserModelAdmin):
    fields = ('name','rank',)
admin.site.register(models.LineType,LineTypeAdmin)


class VehicleAdmin(admin.ModelAdmin):
    pass
    #fields = ('name','rank','type',)
admin.site.register(models.Vehicle,VehicleAdmin)


#
class RidePositionAdmin(UserModelAdmin):

    def get_readonly_fields(self, request, obj=None):
        return ('longitude','latitude','address',)
    def response_change(self, request, obj):
        return HttpWrite.infoSuccess('<script type="text/javascript">alert("保存成功！");self.close()</script>')

    fields = ('city','arriveTime','departureTime','longitude','latitude','address','content','vehicle',)


admin.site.register(models.RidePosition, RidePositionAdmin)

class RidePositionInline(admin.TabularInline):

    #def get_readonly_fields(self, request, obj=None):
    #    return ('longitude','latitude',)
    model = models.RidePosition
    fields = ('longitude','latitude','address','positionType','arriveTime','departureTime','vehicle','rank',)

    extra = 3
    ordering = ['rank']




class RideLineAdmin(admin.ModelAdmin):

    def save_formset(self, request, form, formset, change):
        super(RideLineAdmin,self).save_formset(request,form,formset,change)

        line=form.save(commit=False)

        #line.startPosition=models.RidePosition.objects.get(travelLine=line,positionType=PositionType.objects.get(type=0)).id

        try:
            line.startPosition=models.RidePosition.objects.get(travelLine=line,positionType=models.PositionType.objects.get(type=0)).id
        except MultipleObjectsReturned:
            raise Exception,u"两个起点"
        except:
            pass
        try:
            line.endPosition=models.RidePosition.objects.get(travelLine=line,positionType=models.PositionType.objects.get(type=2)).id
        except MultipleObjectsReturned:
            raise Exception,u"两个终点"
        except:
            pass
        line.save()
    #def get_readonly_fields(self, request, obj=None):
    #def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
    #    if 'lineType'!=db_field.name:
    #        raise Exception,db_field.name
    #    if db_field.name == 'theme':
    #
    #        themeId=request.GET.get('themeId','')
    #
    #        if themeId:
    #            theme=models.BbsTheme.objects.get(id=themeId)
    #            kwargs['initial'] = theme
    #        return db_field.formfield(**kwargs)
    #    return super(RideLineAdmin, self).formfield_for_foreignkey(
    #        db_field, request, **kwargs
    #    )
    def formfield_for_dbfield(self, db_field, **kwargs):

        themeId=kwargs['request'].GET.get('themeId','')
        if db_field.name == 'theme':
            #request=kwargs['request']
            #themeId=request.GET.get('themeId','')
            #raise Exception,themeId
            if themeId:
                theme=models.BbsTheme.objects.get(id=themeId)
                kwargs['initial'] = theme
            #return db_field.formfield(**kwargs)
            #readonly_fields = ('theme',)
        return super(RideLineAdmin,self).formfield_for_dbfield(db_field,**kwargs)

    def response_add(self, request, obj, post_url_continue='../%s/'):
        return HttpResponseRedirect(reverse('bbsTheme_show',args=(obj.theme.id,)))
    def response_change(self, request, obj):
        return HttpResponseRedirect(reverse('bbsTheme_show',args=(obj.theme.id,)))

    fields = ('theme','lineType','startTime','endTime',)
    inlines = [RidePositionInline,]

admin.site.register(models.RideLine, RideLineAdmin)
#






